import tips from '../../lib/tips';
var util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressId: "",
    linkName: "",
    phone: "",
    address: "",
    tipsData: {
      title: '',
      isHidden: true
    },
    openId: app.globalData.openId,
    resource: null,
    transferId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var transferId = "";
    if (options.resource == 2) {
      transferId = options.transferId;
    }
    that.setData({
      resource: options.resource,
      transferId: transferId
    });

    if (options.id) {
      wx.request({
        url: app.globalData.apiDomain + '/api/address/getAddress',
        data: {
          id: options.id
        },
        method: "POST",
        success: function(res) {
          console.log(res.data.data);
          that.setData({
            address: res.data.data.address,
            linkName: res.data.data.linkName,
            phone: res.data.data.phone,
            addressId: res.data.data.id
          });
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  showToast(title, duartion) {
    const that = this;
    const tipsData = {
      title: title || '',
      duartion: duartion || 2000,
      isHidden: false
    };
    tips.toast(that.data.tipsData);
    that.setData({
      tipsData
    });
    setTimeout(() => {
      tipsData.isHidden = true;
      that.setData({
        tipsData
      });
    }, tipsData.duartion);
  },
  //监听标题输入
  listenerLinkNameInput(e) {
    this.data.linkName = e.detail.value;
  },

  //监听标题输入
  listenerPhoneInput(e) {
    this.data.phone = e.detail.value;
  },

  //监听标题输入
  listenerAddressInput(e) {
    this.data.address = e.detail.value;
  },

  formSubmit: util.throttle(function(e) {
    const that = this;
    if (!this.data.linkName) {
      that.showToast('联系人姓名不能为空');
      return;
    }
    if (this.data.linkName.length > 30) {
      that.showToast('联系人姓名不能大于30个字符');
      return;
    }
    if (!this.data.phone) {
      that.showToast('手机号码不能为空');
      return;
    }
    if (!/^1[3|4|5|7|8]\d{9}$/.test(this.data.phone)) {
      that.showToast('手机格式有误，请重新输入');
      return;
    }
    if (!this.data.address) {
      that.showToast('地址不能为空');
      return;
    }
    if (this.data.address.length > 100) {
      that.showToast('地址不能大于100个字符');
      return;
    }

    wx.request({
      url: app.globalData.apiDomain + '/api/address/add',
      method: "POST",
      data: {
        linkName: that.data.linkName,
        phone: that.data.phone,
        address: that.data.address,
        openId: app.globalData.openId,
        nickName: app.globalData.userInfo.nickName
      },
      success: function(res) {
        if (that.data.resource == 1) {
          wx.redirectTo({
            url: '../address/address',
          })
        } else if (that.data.resource == 2) {
          wx.redirectTo({
            url: '../transferDetail/transferDetail?transferId=' + that.data.transferId
          })
        }
        console.log(res);
      },
      complete: function() {

      }
    });
  })
})